export const Global = typeof window !== 'undefined' ? window : global

/*
 * 根据身份证获取出生日期
 * */
export const idCard = (cardNum = '') => {
  // 获取输入身份证号码
  // 获取出生日期
  const birthDate = cardNum.substring(6, 10) + '-' + cardNum.substring(10, 12) + '-' + cardNum.substring(12, 14)
  let sex = 2 // 默认 女
  // 获取性别
  if (parseInt(cardNum.substr(16, 1)) % 2 == 1) {
    sex = 1
  }
  // 获取年龄
  let myDate = new Date()
  let month = myDate.getMonth() + 1
  let day = myDate.getDate()
  let age = myDate.getFullYear() - cardNum.substring(6, 10) - 1
  if (cardNum.substring(10, 12) < month || (cardNum.substring(10, 12) == month && cardNum.substring(12, 14) <= day)) {
    age++
  }
  // 年龄 age
  return {
    birthDate,
    sex,
    age
  }
}

/**
 * 清除所有除了keys的localStorage
 * @param keys
 */
export const clearLocalStorageExclude = (keys = []) => {
  let exclude = {}
  keys.forEach((key) => {
    exclude[key] = localStorage.getItem(key)
  })
  localStorage.clear()
  Object.keys(exclude).forEach((key) => {
    localStorage.setItem(key, exclude[key])
  })
}
